import {layer} from "@layui/layui-vue";

import {StringUtils} from '@/fly/utils/StringUtils';
import useUserRoleInfoBuilder, {UserRoleInfo, UserRoleInfoQuery} from '@/api/admin/model/UserRoleInfoModel';
import EmployeeIdentityApi from "@/api/admin/EmployeeIdentityApi";
import {FormTypeEnum} from "@/fly/biz/BizModel";
import {LayTableUtils} from "@/fly/utils/LayTableUtils";
import useLayTableComposable from "@/fly/table";
import {ref} from "vue";

const {
    userRoleInfo,
    userRoleInfoColumns,
    queryUserRoleInfoParams,
    tableUserRoleInfoData,
    refUserRoleInfoComponent
} = useUserRoleInfoBuilder();
const {flyLayPage} = useLayTableComposable();

const userRoleInfoApi = new EmployeeIdentityApi();


/**
 * 身份信息 处理
 *
 * @author 王大宸
 * @date 2025-01-11 16:32:42
 */
export default function useEmployeeIdentityComposable() {

    const checkedDeptInfo = ref();

    /**
     * 加载分页数据
     * @author 王大宸
     * @param params
     */
    const loadTableUserRoleInfoData = (params: UserRoleInfoQuery) => {
        tableUserRoleInfoData.loading = true;
        userRoleInfoApi.page(params).then(res => {
            tableUserRoleInfoData.total = res.data.total;
            tableUserRoleInfoData.rows = res.data.rows;
            tableUserRoleInfoData.loading = false;
        });
    };

    /**
     * 重新加载数据
     * @author 王大宸
     * @param page
     * @param limit
     */
    const reloadUserRoleInfoDate = (page: number = 1, limit: number = 10) => {
        queryUserRoleInfoParams.page = page;
        queryUserRoleInfoParams.limit = limit;
        loadTableUserRoleInfoData(queryUserRoleInfoParams);
    };

    /**
     * 新增按钮
     */
    const onTapUserRoleInfoAdd = () => {
        refUserRoleInfoComponent.value?.open(FormTypeEnum.ADD, checkedDeptInfo.value, null);
    }

    /**
     * 编辑按钮
     * @param data 编辑时的数据信息
     */
    const onTapUserRoleInfoEdit = (data: UserRoleInfo) => {
        refUserRoleInfoComponent.value?.open(FormTypeEnum.EDIT, checkedDeptInfo.value, data);
    }

    /**
     * 查看按钮
     * @param data 查看时的数据信息
     */
    const onTapUserRoleInfoView = (data: UserRoleInfo) => {
        refUserRoleInfoComponent.value?.open(FormTypeEnum.VIEW, checkedDeptInfo.value, data);
    }

    /**
     * 数据保存处理(新增/编辑)
     * @param data 保存的数据
     */
    const userRoleInfoDataHandler = (data: UserRoleInfo) => {
        if (StringUtils.isEmpty(data.id)) {
            userRoleInfoApi.saveInfo(data).then((res) => {
                reloadUserRoleInfoDate(1, flyLayPage.limit); // 表格重新加载数据
                layer.msg(res.msg, {time: 1500, icon: 1}); // 操作成功提示
                refUserRoleInfoComponent.value?.onTapClose();  // 关闭弹窗
            })
        } else {
            userRoleInfoApi.updateInfo(data, data.id).then((res) => {
                reloadUserRoleInfoDate(1, flyLayPage.limit);  // 表格重新加载数据
                layer.msg(res.msg, {time: 1500, icon: 1}); // 操作成功提示
                refUserRoleInfoComponent.value?.onTapClose();  // 关闭弹窗
            })
        }
    }


    /**
     * 删除按钮
     * @param id 需要删除的数据主键
     */
    const removeUserRoleInfoHandler = (id: string, title?: string) => {
        LayTableUtils.remove({
            title: title,
            callback: () => {
                userRoleInfoApi.remove(id).then((res) => {
                    reloadUserRoleInfoDate(1, flyLayPage.limit);
                    layer.msg(res.msg, {time: 1500, icon: 1});
                })
            }
        })
    }


    return {
        refUserRoleInfoComponent,
        userRoleInfo,
        tableUserRoleInfoData,
        userRoleInfoColumns,
        queryUserRoleInfoParams,
        /* 加载表格 */
        reloadUserRoleInfoDate,
        loadTableUserRoleInfoData,

        /* 新增, 编辑, 查看按钮 */
        onTapUserRoleInfoAdd,
        onTapUserRoleInfoEdit,
        onTapUserRoleInfoView,

        /* 接口 */
        removeUserRoleInfoHandler,
        userRoleInfoDataHandler,
        flyLayPage,
        checkedDeptInfo

    };
}